package com.imooc.demo.service.impl;

import com.imooc.demo.dao.UserAddressInfoDao;
import com.imooc.demo.entity.UserAddressinfo;
import com.imooc.demo.service.UserAddressInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserAddressInfoServiceImpl implements UserAddressInfoService {

    @Autowired
    UserAddressInfoDao userAddressInfoDao;



    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean updateUserAddress(UserAddressinfo userAddressinfo) {
        //空值判断，主要是category1Id不能为空
        if(userAddressinfo.getAddressId()>0&&userAddressinfo.getAddressBuyerPhone()!=null&&userAddressinfo.getAddressArea()!=null&&userAddressinfo.getAddressDetail()!=null) {
            try {
                //更新种类信息
                int effectedNum = userAddressInfoDao.updateUserAddress(userAddressinfo);
                if (effectedNum > 0) {
                    return true;
                } else {
                    throw new RuntimeException("更新地址信息失败！");
                }
            } catch (Exception e) {
                throw new RuntimeException("更新地址信息失败：" + e.toString());
            }
        }else{
            throw new RuntimeException("地址信息不能为空！");
        }
    }

    @Override
    public boolean removeUserAddress(Integer addressId) {
        //空值判断，主要是category1Id不能为空
        if(addressId!=null) {
            try {
                //更新种类信息
                int effectedNum = userAddressInfoDao.removeUserAddress( addressId);
                if (effectedNum > 0) {
                    return true;
                } else {
                    throw new RuntimeException("删除地址信息失败！");
                }
            } catch (Exception e) {
                throw new RuntimeException("删除地址信息失败：" + e.toString());
            }
        }else{
            throw new RuntimeException("地址信息不能为空！");
        }
    }

    @Override
    public boolean insertUserAddressinfo(UserAddressinfo userAddressinfo) {
        if(userAddressinfo.getAddressUserName()!=null && !"".equals(userAddressinfo.getAddressUserName())&&
                userAddressinfo.getAddressBuyerPhone()!=null && !"".equals(userAddressinfo.getAddressBuyerPhone())&&
                userAddressinfo.getAddressArea()!=null && !"".equals(userAddressinfo.getAddressArea())&&
                userAddressinfo.getAddressDetail()!=null&&!"".equals(userAddressinfo.getAddressDetail())&&
                userAddressinfo.getBuyerOpenid()!=null && !"".equals(userAddressinfo.getBuyerOpenid())){
            try{
                int effectedNum = userAddressInfoDao.insertUserAddressinfo(userAddressinfo);
                if(effectedNum>0){
                    return  true;
                }else{
                    throw  new RuntimeException("插入地址信息失败！");
                }
            }catch (Exception e){
                throw new RuntimeException("插入地址信息失败！");
            }
        }else{
            throw new RuntimeException("地址不能为空！");
        }
    }

    @Override
    public UserAddressinfo getAddressinfoById(Integer addressId) {
        return userAddressInfoDao.getAddressinfoById(addressId);
    }


}
